﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LinqToEdmx;
using System.IO;
using System.Configuration;
using System.Xml;

namespace OperateEdmx
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        #region 事件
        private void menuOpne_Click(object sender, EventArgs e)
        {
            OpenFile();
        }
        
        
        private void btnSetMaxLength_Click(object sender, EventArgs e)
        {
            //SetMaxLengthToEntityFromStorage();
            Test1();
        }
        #endregion

        #region 变量
        LinqToEdmx.Edmx edmx;
        string edmxpath;
        #endregion

        #region 方法
        private void OpenFile()
        {
            if (openFileDialogMain.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                edmxpath = openFileDialogMain.FileName;
                LoadEdmx(edmxpath);
            }
        }

        private void LoadEdmx(string FileName)
        {
            edmx = LinqToEdmx.Edmx.Load(FileName);
            Test();
        }


        //Dictionary<string, object> dctMaxLength;
        private void Test()
        {
            //dctMaxLength = new Dictionary<string, object>();

            //IEnumerable<LinqToEdmx.Model.Storage.StorageSchema> ss = this.edmx.GetItems<LinqToEdmx.Model.Storage.StorageSchema>();
            //foreach (LinqToEdmx.Model.Storage.StorageSchema s in ss)
            //{
            //    foreach (LinqToEdmx.Model.Storage.EntityTypeStore ets in s.EntityTypeStores)
            //    {
            //        foreach(LinqToEdmx.Model.Storage.EntityProperty p in ets.Properties)
            //        {
            //            if (p.MaxLength != null)
            //                dctMaxLength.Add(string.Format("{0}.{1}", ets.Name, p.Name), p.MaxLength);
            //        }
                    
            //        //Console.WriteLine(ets.Name);
            //        //et.Name = this.FormatClassName(et.Name);
            //    }
            //}


    //IEnumerable<LinqToEdmx.Model.Conceptual.EntityContainer> ec = this.edmx.GetItems<LinqToEdmx.Model.Conceptual.EntityContainer>();
    //Console.WriteLine("修改EntityContainer--EntitySets--EntityType");
    //foreach (LinqToEdmx.Model.Conceptual.EntityContainer e in ec)
    //{
    //    foreach (LinqToEdmx.Model.Conceptual.EntityContainer.EntitySetLocalType es in e.EntitySets)
    //    {
    //        Console.WriteLine(es.EntityType);
    //        es.EntityType = this.FormatClassName(es.EntityType);
    //    }
    //}

    //Console.WriteLine("修改ConceptualModels--Schema--EntityType的Name");
    //IEnumerable<ConceptualSchema> cs = this.edmx.GetItems<ConceptualSchema>();
    //foreach (ConceptualSchema q in cs)
    //{
    //    foreach (EntityType et in q.EntityTypes)
    //    {
    //        Console.WriteLine(et.Name);
    //        et.Name = this.FormatClassName(et.Name);
    //    }
    //}

    //Console.WriteLine("修改EntityTypeMapping-TypeName");
    //IEnumerable<EntityTypeMapping> esm = this.edmx.GetItems<EntityTypeMapping>();
    //foreach (EntityTypeMapping q2 in esm)
    //{
    //    Console.WriteLine(q2.TypeName);
    //    q2.TypeName = this.FormatClassName(q2.TypeName);
    //}

    //Console.WriteLine("修改EntityTypeShape--EntityType");
    //IEnumerable<EntityTypeShape> ets = this.edmx.GetItems<EntityTypeShape>();
    //foreach (EntityTypeShape q3 in ets)
    //{
    //    Console.WriteLine(q3.EntityType);
    //    q3.EntityType = this.FormatClassName(q3.EntityType);
    //}
    //Console.WriteLine("修改Association--Ends--Type");
    //IEnumerable<Association> ass = this.edmx.GetItems<Association>();
    //foreach (Association q4 in ass)
    //{
    //    foreach (AssociationEnd end in q4.Ends)
    //    {
    //        Console.WriteLine(end.Type);
    //        end.Type = this.FormatClassName(end.Type);
    //    }
    //}
    //Console.WriteLine("修改Diagram--EntityTypeShapes--EntityType");
    //IEnumerable<Diagram> dia = this.edmx.GetItems<Diagram>();
    //foreach (Diagram d in dia)
    //{
    //    foreach (EntityTypeShape et2 in d.EntityTypeShapes)
    //    {
    //        Console.WriteLine(et2.EntityType);
    //        et2.EntityType = this.FormatClassName(et2.EntityType);
    //    }
    //}
        }

        private void SetMaxLengthToEntityFromStorage()
        {
            //定义一个字典来存放Storage中表字段的字段长度
            Dictionary<string, object> dctMaxLength = new Dictionary<string, object>();
            IEnumerable<LinqToEdmx.Model.Storage.StorageSchema> ss = edmx.GetItems<LinqToEdmx.Model.Storage.StorageSchema>();

            foreach (LinqToEdmx.Model.Storage.StorageSchema s in ss)
            {
                foreach (LinqToEdmx.Model.Storage.EntityTypeStore ets in s.EntityTypeStores)
                {
                    foreach (LinqToEdmx.Model.Storage.EntityProperty p in ets.Properties)
                    {
                        if (p.MaxLength != null)
                            dctMaxLength.Add(string.Format("{0}.{1}", ets.Name, p.Name), p.MaxLength);
                    }
                }
            }

            IEnumerable<LinqToEdmx.Model.Conceptual.ConceptualSchema> cs = this.edmx.GetItems<LinqToEdmx.Model.Conceptual.ConceptualSchema>();
            foreach (LinqToEdmx.Model.Conceptual.ConceptualSchema q in cs)
            {
                foreach (LinqToEdmx.Model.Conceptual.EntityType et in q.EntityTypes)
                {
                    foreach (LinqToEdmx.Model.Conceptual.EntityProperty p in et.Properties)
                    {
                        string key = string.Format("{0}.{1}", et.Name, p.Name);
                        if (dctMaxLength.ContainsKey(key))
                            p.MaxLength = dctMaxLength[key];
                    }
                }
            }

            edmx.Save(edmxpath);
        }
        #endregion

        

        private void Test1()
        {
            string xmlpath = @"c:\Data.xml";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlpath);

            //XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.Element, a1, a2);
            //xmlnode.InnerText = "hahahh";
            //xmldoc.FirstChild.ChildNodes[1].ChildNodes[3].FirstChild.ChildNodes[1].AppendChild(xmlnode);
            //xmldoc.Save(xmlpath);

            //foreach (System.Xml.XmlElement x in xmldoc.FirstChild.ChildNodes[1].ChildNodes[3].FirstChild.ChildNodes)
            //{
            //    if (x.Name == "EntityType")
            //    {
            //        string xname = x.Attributes["Name"].Value;

            //        //XmlNode xmlnode = xmldoc.CreateNode(a1, "Test", "13131");
            //        //x.AppendChild(xmlnode);
            //    }
            //}

        }

    }
}
